package com.bank.um.controller;

import com.bank.RabbitMQ.Producter;
import com.bank.entity.Member;
import com.bank.entity.User;
import com.bank.entity.constant.ResponseEnum;
import com.bank.entity.vo.Response;
import com.bank.um.service.MemberService;
import com.bank.um.service.UserService;
import com.bank.utils.CookieUtils;
import com.bank.utils.UUIDUtil;
import com.bank.utils.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.bank.utils.ValidatorUtils.GetCode;
import static com.bank.utils.ValidatorUtils.getRemainSecondsDay;

/**
 * @author: Lixy
 * @email: 1908598362@qq.com
 * @date: 2021/12/31 星期五 20:33
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private MemberService memberService;


    @GetMapping("/allMem")
    public List<Member> findAllMember() {
        return memberService.listAll();
    }

    @GetMapping("/allUsr")
    public List<User> findAllUser() {
        return userService.listAll();
    }

    @PostMapping("/regist")
    public Response regist(String name, Long memcardnum, String password) {
        String Md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        //String sms = sendService.sendSms(phone);
        //Response response = new Response();
        int row = userService.addUser(name, memcardnum,Md5Password);
        if (Md5Password.isEmpty()) {
            Response response1 =  Response.response1(ResponseEnum.ERROR,-1,"密码不能为空");
            return response1;
        }
        if (name == null) {
            Response response1 = Response.response1(ResponseEnum.ERROR,-1,"姓名不能为空");
            return response1;
        }
        if (memcardnum == null) {
            Response response1 =  Response.response1(ResponseEnum.ERROR,-1,"身份证不能为空");
            return response1;
        }
        if (row > 0) {
            Response response1 = Response.response1(ResponseEnum.SUCCESS, row, "注册成功");
            return response1;
        } else {
            Response response1 = Response.response1(ResponseEnum.ERROR,-1,"请检查验证码是否正确或网络是否正常");
            return response1;
        }
    }


    @PostMapping("/loginByPass")
    public Response loginByPass(@Valid String name, String password, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest){
        String Md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        //Response response = new Response();
        User user = userService.SelectById(name, Md5Password);
        if (user!=null){
            //jjwt设置Token令牌解决短会话问题
            //httpServletResponse.setHeader("Token", JwtUtils.getJwtToken(String.valueOf(member.getId())));

            //用户信息存入Session中，通过Cookie存储
            String uuid = UUIDUtil.uuid();
            //httpServletRequest.getSession().setAttribute(uuid,member);
            redisTemplate.opsForValue().set("user:"+uuid,user,1800,TimeUnit.SECONDS);
            CookieUtils.setCookie(httpServletRequest,httpServletResponse,"UserUUID",uuid);
            return Response.response3(ResponseEnum.SUCCESS,"登陆成功");
        }else {
            return Response.response3(ResponseEnum.CHECK_USERNAME_AND_PASSWORD_ERROR,ResponseEnum.CHECK_USERNAME_AND_PASSWORD_ERROR.getMsg());
        }
    }

    @RequestMapping("/info")
    @ResponseBody
    public Member info(String memberId){
        Member member = memberService.SelectById(memberId);
        return member;
    }
}

